﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace AMS.Configuration
{
    /// <summary>
    /// 
    /// </summary>
    public static class AmsEnvironment
    {
        static string startupDirectory;
        static string appRoot;

        /// <summary>
        /// APP程序运行的根目录的全路径。以\结尾：类似：c:\program files\cme\
        /// 来自黑米的博客：qkhh.cnblogs.com
        /// </summary>
        public static string StartupDirectory
        {
            get
            {
                if (string.IsNullOrEmpty(startupDirectory))
                {
                    string exeFileDirectory = Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
                    if (exeFileDirectory.EndsWith(@"\", StringComparison.OrdinalIgnoreCase))
                        startupDirectory = exeFileDirectory;
                    else
                        startupDirectory = exeFileDirectory + @"\";
                }

                return startupDirectory;
            }
        }


        /// <summary>
        /// 应用程序的根目录，实际上是StartupDirectory的上级目录。以\结尾
        /// </summary>
        public static string AppRoot
        {
            get
            {
                if (string.IsNullOrEmpty(appRoot))
                {
                    appRoot = Path.GetFullPath(StartupDirectory + @"..");
                    if (!appRoot.EndsWith(@"\"))
                    {
                        appRoot += @"\";
                    }
                }

                return appRoot;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetSystemPath(SystemPath path)
        {
            switch (path)
            {
                case SystemPath.Bin: return StartupDirectory;
                case SystemPath.Etc: return AppRoot + @"etc\";
                case SystemPath.Data: return AppRoot + @"data\";
                case SystemPath.Log: return AppRoot + @"log\";
                case SystemPath.InterfaceFile: return AppRoot + @"etc\interface.ini";
                case SystemPath.DictFile: return AppRoot + @"etc\dict.dat";
                default:
                    throw new NotImplementedException();
            }
        }
    }
}
